package com.controller;

import java.util.List;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dao.LogDao;
import com.dao.UserDao;
import com.mysql.jdbc.log.Log;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;
import com.util.MybatisSessionFactory;

@Controller
public class UserController {
	@RequestMapping(value = "register.action", method = RequestMethod.POST)
	@ResponseBody
	public String register(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			@RequestParam(value = "password2", defaultValue = "") String password2) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao dao = session.getMapper(UserDao.class);
		LogDao dao1 = session.getMapper(LogDao.class);
		Map<String, Object> result = dao.query(account);
		if (!password.equals(password2)) {
			return "NO";
		}

		if ("1".equals(result.get("counter").toString())) {
			return "ERR";
		}
		dao.add(account, password, account, "2012-06-03");
		Map<String, Object> result1 = dao.queryid();
		dao1.insert(result1.get("id").toString(), "0", "注册");
		session.commit();
		return "OK";
	}

	@RequestMapping(value = "login.action", method = RequestMethod.POST)
	@ResponseBody
	public String login(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao c = session.getMapper(UserDao.class);
		LogDao dao1 = session.getMapper(LogDao.class);
		Map<String, Object> result = c.queryall(account, password);
		if (!"1".equals(result.get("counter").toString())) {
			return "ERR";
		}
		response.addCookie(new Cookie("user", result.get("id").toString()));
		dao1.insert(result.get("id").toString(), "0", "登录");
		session.commit();
		return "OK";
	}

	@RequestMapping(value = "quit.action", method = RequestMethod.POST)
	@ResponseBody
	public String quit(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao c = session.getMapper(UserDao.class);
		LogDao dao1 = session.getMapper(LogDao.class);
		dao1.insert(userId, "0", "退出");
		response.addCookie(new Cookie("user", null));
		session.commit();
		return "OK";
	}

	@RequestMapping(value = "queryuser.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> queryuser(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		if ("".equals(userId)) {
			try {
				response.sendRedirect("login.html");
				return null;
			} catch (Exception e) {
				System.out.println("[error] queryUser.action:redirect\r\n");
				return null;
			}
		}
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		Map<String, Object> map = d.queryuser(userId);
		return map;
	}

	@RequestMapping(value = "query_moneyin.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> querydatei(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		Map<String, Object> map = d.query_moneyin(userId);
		return map;
	}

	@RequestMapping(value = "query_moneyout.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> querydateo(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		Map<String, Object> map = d.query_moneyout(userId);
		return map;
	}

	@RequestMapping(value = "queryJinQi.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> queryJinQi(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		List<Map> map = d.queryJinQi(userId);
		return map;
	}

	@RequestMapping(value = "/add2.action", method = RequestMethod.POST)
	@ResponseBody
	public String add2(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response,
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "money_in", defaultValue = "") String money_in,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "remark", defaultValue = "") String remark) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao dao = session.getMapper(UserDao.class);
		LogDao dao1 = session.getMapper(LogDao.class);
		dao.queryadd(userId, date_time, money_in, place, kind, remark);
		Map<String, Object> result1 = dao.queryid();
		dao1.insert(userId, result1.get("id").toString(), "记账收入");
		session.commit();
		return "OK";
	}

	@RequestMapping(value = "/add1.action", method = RequestMethod.POST)
	@ResponseBody
	public String add1(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response,
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "money_out", defaultValue = "") String money_out,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "remark", defaultValue = "") String remark) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao dao = session.getMapper(UserDao.class);
		LogDao dao1 = session.getMapper(LogDao.class);
		dao.queryadd1(userId, date_time, money_out, place, kind, remark);
		Map<String, Object> result1 = dao.queryid();
		dao1.insert(userId, result1.get("id").toString(), "记账支出");
		session.commit();
		return "OK";
	}

	@RequestMapping(value = "pin5.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> pin5(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		Map<String, Object> map = d.pin5(userId);
		return map;
	}

	@RequestMapping(value = "pin6.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> pin6(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		Map<String, Object> map = d.pin6(userId);
		return map;
	}

	@RequestMapping(value = "queryg.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> queryg(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		List<Map> map = d.queryg(userId);
		return map;
	}

	@RequestMapping(value = "mohu.action", method = RequestMethod.POST)
	@ResponseBody
	public List<Map> mohu(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response,
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "money_in", defaultValue = "") String money_in,
			@RequestParam(value = "money_out", defaultValue = "") String money_out,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "remark", defaultValue = "") String remark
	) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao c = session.getMapper(UserDao.class);
		List<Map> map = c.mohu(userId, date_time, place, money_in, money_out,
				kind, remark);
		System.out.println(userId + date_time + place + money_in + money_out
				+ kind + remark);
		session.commit();
		return map;
	}

	@RequestMapping(value = "xiang.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> xiang(
			@CookieValue(value = "user", defaultValue = "") String userId,
			@RequestParam(value = "id", defaultValue = "") String id,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		List<Map> map = d.xiang(id, userId);
		return map;
	}

	@RequestMapping(value = "dele.action", method = RequestMethod.POST)
	@ResponseBody
	public String dele(
			@CookieValue(value = "user", defaultValue = "") String userId,
			@RequestParam(value = "id", defaultValue = "") String id,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao dao = session.getMapper(UserDao.class);
		dao.dele(userId, id);
		LogDao dao1 = session.getMapper(LogDao.class);
		dao1.insert(userId, id, "删除");
		session.commit();
		return "OK";
	}

}
